package com.imooc.seataorder;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {

    private final ProductFeignClient productFeignClient;

/*
    private final RestTemplate restTemplate;
*/

    @GlobalTransactional
    public Boolean create(Integer count) {

     /*   //调用product 扣库存
        String url = "http://localhost:8086/deduct?productId=111&count=" + count;
        Boolean result = restTemplate.getForObject(url, Boolean.class);*/

         Boolean result = productFeignClient.deduct(111L, count);

        if (result != null && result) {
            //可能抛出异常
            if (1 == count) {
                throw new RuntimeException("order发生异常了");
            }

            log.info("数据库开始创建订单");
            return true;
        }
        return false;
    }
}
